#! /usr/bin/ruby

puts "Hello world!"

system "echo 'Hello, Matz!'" # 执行系统命令

puts "Hello, " << "Matz!"

puts "Hello, Matz! " * 3

5.times {print "Hello, Matz!\n"}

puts "Hey Matz, I'm running " + `ruby --version`


puts '-- ' * 10 << __LINE__.to_s 

hi = "Hello, Ruby!"
puts hi

puts self.class.ancestors.to_s # [Object, Kernel, BasicObject]

puts self.methods.to_s
=begin 
[
  :to_s, :inspect, :hash, :singleton_class, :dup, :itself, :methods, 
  :singleton_methods, :protected_methods, :private_methods, :public_methods, 
  :instance_variables, :instance_variable_get, :instance_variable_set, 
  :instance_variable_defined?, :remove_instance_variable, :instance_of?, 
  :kind_of?, :is_a?, :display, :public_send, :extend, :clone, 
  :<=>, :class, :===, :!~, :frozen?, :then, :tap, :nil?, :yield_self, 
  :eql?, :respond_to?, :method, :public_method, :singleton_method, 
  :define_singleton_method, :freeze, :object_id, :send, :to_enum, 
  :enum_for, :!, :equal?, :__id__, :__send__, :==, :!=, :instance_eval, :instance_exec
  ]
=end
  
puts `clear`

hi = "Hello, "
r ="Ruby!"

puts hi + r

puts "Hello, #{2000+25}"
puts "Hello, #{r}"

puts '-- ' * 10 << __LINE__.to_s 

puts `clear` 

hi = "Hello, %s"
puts hi % "Ruby!"
puts hi % "people!"

puts "%s, %s" % ["Hello","Ruby!"]

sprintf("Hello, %s","Ruby!")
printf("Hello, %s", "Ruby!!\n")

puts '-- ' * 10 << __LINE__.to_s 

puts `clear` 

puts '-- ' * 10 << __LINE__.to_s 

puts "Test Git"
puts "Test Git"
# git config --global credential.helper store  
# 缓存 账号

puts '-- ' * 10 << __LINE__.to_s 
puts `clear` 

# hello = gets
# puts "hello, " + hello

puts '-- ' * 10 << __LINE__.to_s 
puts `clear` 

def hello
  puts "Hello, Ruby!"
end

hello

def hello
  puts self # main
  yield 
end

hello { puts "hello, Ruby!"}

puts '-- ' * 10 << __LINE__.to_s 
puts `clear` 

["hello, ","Ruby!"].each {|e| print e}
puts 

(lambda {|name| puts "hello, "+ name}).call "Ruby2025!"

require "rexml/document"

file = File.new "hello.xml"
doc = REXML::Document.new file
puts doc.to_s

puts '-- ' * 10 << __LINE__.to_s 
puts `clear` 

class Hello
  def initialize(name)
    @name=name  
  end

  def hello_ruby
    puts "Hello, #{@name}!"
  end
end

hi = Hello.new "Ruby--2025!"
hi.hello_ruby

puts `clear` 

class Point
  def initialize(x,y)
    @x, @y = x, y
    p self # #<Point:0x00007dd93a0c5530 @y=20, @x=10>  实例
  end

  def to_s
    "(#@x, #@y)"
  end

  def x 
    @x
  end

  # 写者方法
  def x=(value)
    @x=value
  end

  def y 
    @y
  end

  # 写者方法
  def y=(value)
    @y=value
  end

  def +(other)
    Point.new(@x + other.x, @y + other.y)
  end

  def -@
    Point.new(-@x, -@y)
  end
end

# p Point.object_id # 60

p = Point.new 10,20
p p.class # Point
p p.is_a? Point # true
p p # #<Point:0x00007dd93a0c5530 @y=20, @x=10>

puts p # (10, 20)

puts p.x # 10
puts p.y # 20

p.x = 0
p.y = 0

p p.x, p.y # 0 0

puts `clear` 

